#include "app_gpiote.h"
#include "app_io.h"
#include "dspi_screen_390.h"
#include "app_pwm.h"
#include "board_SK.h"

static void dspi_screen_set_show_area(uint16_t x1, uint16_t x2, uint16_t y1, uint16_t y2);


app_pwm_params_t pwm_params = {
    .id = APP_PWM1_MODULE,
    .pin_cfg = {
        .channel_a = {
            .type   = APP_PWM1_GPIO_TYPE,
            .mux    = APP_PWM1_GPIO_MUX,
            .pin    = APP_PWM1_CHANNEL_A,
            .pull   = APP_IO_NOPULL,
            .enable = APP_PWM_PIN_ENABLE,
        },
    },
    .active_channel = APP_PWM_ACTIVE_CHANNEL_A,
    .init = {
        .mode     = PWM_MODE_FLICKER,
        .align    = PWM_ALIGNED_EDGE,
        .freq     = 100,
        .bperiod  = 500,
        .hperiod  = 200,
        .bstoplvl = PWM_STOP_LVL_LOW,
        .channel_a = {
            .duty = 30,
            .drive_polarity = PWM_DRIVEPOLARITY_POSITIVE,
            .fstoplvl = PWM_STOP_LVL_LOW,

        },

    },
};

app_dspi_params_t dspi_params = {
    .pin_cfg = {
        .cs = {				
					 .type   = APP_IO_TYPE_MSIO,
           .mux    = APP_IO_MUX_7,
           .pin    = APP_IO_PIN_1,
           // .mode   = APP_IO_MODE_OUTPUT,
           .pull   = APP_IO_PULLUP,
           .enable = APP_DSPI_CS_ENABLE,
        },
        .clk = {
					  .type   = APP_DSPI_CLK_TYPE,
            .mux    = APP_DSPI_CLK_PIN_MUX,
            .pin    = APP_DSPI_CLK_PIN,
            .pull   = APP_DSPI_CLK_PULL,
            .enable = APP_DSPI_CLK_ENABLE,
        },
        .mosi = {
					
					 .type   = APP_DSPI_MOSI_TYPE,
            .mux    = APP_DSPI_MOSI_PIN_MUX,
            .pin    = APP_DSPI_MOSI_PIN,
            .pull   = APP_DSPI_MOSI_PULL,
            .enable = APP_DSPI_MOSI_ENABLE,
        },
        .miso = {
            .type   = APP_DSPI_MISO_TYPE,
            .mux    = APP_DSPI_MISO_PIN_MUX,
            .pin    = APP_DSPI_MISO_PIN,
            .pull   = APP_DSPI_MISO_PULL,
            .enable = APP_DSPI_MISO_ENABLE,
        },
        .dcx = {
            .type   = APP_IO_TYPE_GPIOB,
            .mux    = APP_DSPI_DCX_PIN_MUX,
            .pin    = APP_DSPI_DCX_PIN,
            .pull   = APP_DSPI_DCX_PULL,
            .enable = APP_DSPI_DCX_ENABLE,
        },
    },
    .dma_cfg.channel = DMA_Channel0,
    .init = {
        .data_size = DSPI_DATASIZE_08_BITS,
        .baud_rate = DSPI_BAUD_RATE_2P1PCLK,
        .dspi_mode = DSPI_PROT_MODE_3W1L,
    },
    .is_soft_cs = true,
};
/* misc pins */

#define DISP_RST_CONFIG                  { APP_IO_TYPE_MSIO, APP_IO_PIN_0, APP_IO_MODE_OUTPUT,  APP_IO_NOPULL, NULL}
#define DISP_TE_CONFIG                   { APP_IO_TYPE_AON, APP_IO_PIN_5, APP_IO_MODE_INPUT,   APP_IO_NOPULL, NULL}
#define DISP_DC_CONFIG                   { APP_IO_TYPE_AON, APP_IO_PIN_7, APP_IO_MODE_INPUT,   APP_IO_NOPULL, NULL}

/*
 * LOCAL VARIABLE DEFINITIONS
 ***************************************************************
 *
 */
static volatile uint8_t                     g_master_tdone = 0;
static dspi_command_t                       g_cmd;
volatile uint8_t                            display_busy = 0;
static volatile uint32_t                    display_xfer_remain = 0;
static uint8_t                              *display_xfer_point = NULL;
#define DSPI_DMA_SIZE                       (960)

/*
 * LOCAL FUNCTION
 ***************************************************************
 *
 */
static void Solomon_VPA(unsigned char data)
{
	app_dspi_dma_transmit_async(&data,1);
	while(g_master_tdone == 0);
}
static void Solomon_VMD(unsigned char cmd)
{
		g_cmd.instruction = cmd;
    g_cmd.instruction_size = DSPI_INSTSIZE_08_BITS;
    g_cmd.data_size = DSPI_DATASIZE_08_BITS;
    g_cmd.length = 1;

    g_master_tdone = 0;
    app_dspi_dma_command_async(&g_cmd);
    while(g_master_tdone == 0);
}
static void Solomon_VPA_2(unsigned short data)
{
	app_dspi_dma_transmit_async((uint8_t *)&data,2);
	while(g_master_tdone == 0);
}
static void Solomon_VMD_2(unsigned short cmd)
{
		g_cmd.instruction = cmd;
    g_cmd.instruction_size = DSPI_INSTSIZE_16_BITS;
    g_cmd.data_size = DSPI_DATASIZE_16_BITS;
    g_cmd.length = 2;

    g_master_tdone = 0;
    app_dspi_dma_command_async(&g_cmd);
    while(g_master_tdone == 0);
}
void send_cmd_data(uint8_t cmd,int len,uint8_t *pdata)
{
	  g_cmd.instruction = cmd;
    g_cmd.instruction_size = DSPI_INSTSIZE_08_BITS;
    g_cmd.data_size = DSPI_DATASIZE_08_BITS;
    g_cmd.length = len;

    app_dspi_dma_command_transmit_async(&g_cmd,pdata);
}
void init_seq(void)
{
Solomon_VMD(0xDF); //Password
Solomon_VPA(0x98);
Solomon_VPA(0x51);
Solomon_VPA(0xE9);

Solomon_VMD(0xDE);
Solomon_VPA(0x00);

Solomon_VMD(0xB7);
Solomon_VPA(0x1E);
Solomon_VPA(0x85);
Solomon_VPA(0x21);
Solomon_VPA(0x30);

Solomon_VMD(0xC8);     //gamma2.5     //gamma2.0
Solomon_VPA(0x3F);     //0x3F         //0x35
Solomon_VPA(0x30);     //0x2E         //0x32
Solomon_VPA(0x27);     //0x25         //0x2F
Solomon_VPA(0x24);     //0x21         //0x32
Solomon_VPA(0x26);     //0x23         //0x37
Solomon_VPA(0x28);     //0x24         //0x3F
Solomon_VPA(0x23);     //0x1F         //0x3D
Solomon_VPA(0x22);     //0x1F         //0x3E
Solomon_VPA(0x22);     //0x1E         //0x3D
Solomon_VPA(0x22);     //0x1E         //0x3D
Solomon_VPA(0x20);     //0x1B         //0x3B
Solomon_VPA(0x17);     //0x11         //0x2E
Solomon_VPA(0x13);     //0x0C         //0x28
Solomon_VPA(0x0E);     //0x06         //0x1F
Solomon_VPA(0x08);     //0x02         //0x17
Solomon_VPA(0x08);     //0x08         //0x0E
Solomon_VPA(0x3F);     //0x3F         //0x35
Solomon_VPA(0x30);     //0x2F         //0x31
Solomon_VPA(0x27);     //0x26         //0x2F
Solomon_VPA(0x24);     //0x22         //0x31
Solomon_VPA(0x26);     //0x24         //0x36
Solomon_VPA(0x28);     //0x25         //0x3E
Solomon_VPA(0x23);     //0x20         //0x3D
Solomon_VPA(0x22);     //0x1F         //0x3E
Solomon_VPA(0x22);     //0x1F         //0x3D
Solomon_VPA(0x22);     //0x1F         //0x3D
Solomon_VPA(0x20);     //0x1C         //0x3B
Solomon_VPA(0x17);     //0x11         //0x2E
Solomon_VPA(0x13);     //0x0C         //0x28
Solomon_VPA(0x0E);     //0x06         //0x1F
Solomon_VPA(0x08);     //0x02         //0x17
Solomon_VPA(0x08);     //0x08         //0x0E

//Solomon_VMD(0x36);
//Solomon_VPA(0x0A);
Solomon_VMD(0xB9);
Solomon_VPA(0x33);
Solomon_VPA(0x28);
Solomon_VPA(0xCC);

Solomon_VMD(0xBB);
Solomon_VPA(0x0A);
Solomon_VPA(0x39);
Solomon_VPA(0x20);
Solomon_VPA(0x30);
Solomon_VPA(0x6C);
Solomon_VPA(0x60);
Solomon_VPA(0x60);
Solomon_VPA(0x70);

Solomon_VMD(0xBC);
Solomon_VPA(0x38);
Solomon_VPA(0x3C);

Solomon_VMD(0xC0);
Solomon_VPA(0x12);
Solomon_VPA(0x20);

Solomon_VMD(0xC1);
Solomon_VPA(0x12);

Solomon_VMD(0xC2);
//Solomon_VPA(0x08);

Solomon_VPA(0x35);
Solomon_VPA(0x00);

Solomon_VMD(0xC3);
Solomon_VPA(0x08);
Solomon_VPA(0x00);
Solomon_VPA(0x0A);
Solomon_VPA(0x10);
Solomon_VPA(0x08);
Solomon_VPA(0x68);
Solomon_VPA(0x45);
Solomon_VPA(0x81);
Solomon_VPA(0x2C);

Solomon_VMD(0xC4);
Solomon_VPA(0x00);
Solomon_VPA(0x8E);
Solomon_VPA(0x87);
Solomon_VPA(0x10);
Solomon_VPA(0x0A);
Solomon_VPA(0x16);
Solomon_VPA(0x87);
Solomon_VPA(0x0E);
Solomon_VPA(0x0A);
Solomon_VPA(0x16);
Solomon_VPA(0x87);
Solomon_VPA(0x0E);
Solomon_VPA(0x0A);
Solomon_VPA(0x16);
Solomon_VPA(0x82);
Solomon_VPA(0x00);
Solomon_VPA(0x03);


Solomon_VMD(0xD0);
Solomon_VPA(0x04);
Solomon_VPA(0x0C);
Solomon_VPA(0x7B);
Solomon_VPA(0x0F);
Solomon_VPA(0x01);
Solomon_VPA(0x03);

Solomon_VMD(0xD7);
//Solomon_VPA(0x20);
Solomon_VPA(0x00);// 20  2DATA
Solomon_VPA(0x00);

//app_dspi_config_mode(DSPI_PROT_MODE_4W2L);
//app_dspi_config_data_size(DSPI_DATASIZE_16_BITS);
//Solomon_VMD_2(0xDE00);
//Solomon_VPA_2(0x0200);

//Solomon_VMD_2(0xC400);
//Solomon_VPA_2(0x7A04);
//Solomon_VMD_2(0xB800);
//Solomon_VPA_2(0x1DA0);
//Solomon_VPA_2(0x2F04);
//Solomon_VPA_2(0x2400);
//Solomon_VMD_2(0xB200);
//Solomon_VPA_2(0x30F8);
//Solomon_VPA_2(0x936E);
//Solomon_VMD_2(0xC100);
//Solomon_VPA_2(0x1066);
//Solomon_VPA_2(0x6601);
//Solomon_VMD_2(0xDE00);
//Solomon_VPA_2(0x0);
//Solomon_VPA_2(0x6601);
//Solomon_VMD_2(0x3A00);
//Solomon_VPA_2(0x0500);
//Solomon_VMD_2(0x1100);
//delay_ms(120);
//Solomon_VMD_2(0x2A00);
//Solomon_VPA_2(0x0000);
//Solomon_VPA_2(0x00EF);
//Solomon_VMD_2(0x2B00);
//Solomon_VPA_2(0x0000);
//Solomon_VPA_2(0x011A);
//Solomon_VMD_2(0x2C00);
//Solomon_VMD_2(0x2900);
//Solomon_VMD(0x36);
//Solomon_VPA(0x0A);

Solomon_VMD(0xDE);
Solomon_VPA(0x02);


Solomon_VMD(0xC4);
Solomon_VPA(0x7A);
Solomon_VPA(0x04);

Solomon_VMD(0xB8);
Solomon_VPA(0x1D);
Solomon_VPA(0xA0);
Solomon_VPA(0x2F);
Solomon_VPA(0x04);
Solomon_VPA(0x24);

Solomon_VMD(0xB2);
Solomon_VPA(0x30);
Solomon_VPA(0xF8);
Solomon_VPA(0x93);
Solomon_VPA(0x6E);

Solomon_VMD(0xC1);
Solomon_VPA(0x10);
Solomon_VPA(0x66);
Solomon_VPA(0x66);
Solomon_VPA(0x01);

Solomon_VMD(0xDE);
Solomon_VPA(0x00);

Solomon_VMD(0x3A);
Solomon_VPA(0x05); 	   //rgb 666 bit

Solomon_VMD(0x11);  	 // SLPOUT
delay_ms(120);

Solomon_VMD(0x2A);
Solomon_VPA(0x00);
Solomon_VPA(0x00);
Solomon_VPA(0x00);
Solomon_VPA(0xEF);

Solomon_VMD(0x2B);
Solomon_VPA(0x00);
Solomon_VPA(0x00);
Solomon_VPA(0x01);
Solomon_VPA(0x1A);

Solomon_VMD(0x2C);

Solomon_VMD(0x29);  	// SLPOUT

}
static void app_dspi_display_xfer_start(uint8_t *data, uint32_t dataLen)
{
g_master_tdone = 0;
app_dspi_dma_transmit_async(data, dataLen);

}

static void app_dspi_evt_handler(app_dspi_evt_t *p_evt)
{
if (p_evt->type == APP_DSPI_EVT_TX_CPLT)
{
	g_master_tdone = 1;
	if (display_busy == 1)
	{
			if (display_xfer_remain != 0)
			{
					if (display_xfer_remain > DSPI_DMA_SIZE)
					{
							app_dspi_display_xfer_start(display_xfer_point, DSPI_DMA_SIZE);
							display_xfer_remain -= DSPI_DMA_SIZE;
							display_xfer_point += DSPI_DMA_SIZE;
					}
					else
					{
							app_dspi_display_xfer_start(display_xfer_point, DSPI_DMA_SIZE);
							display_xfer_remain = 0;
							display_xfer_point += display_xfer_remain;
					}
			}
			else
			{
					display_busy = 0;
			}
	}
}

if (p_evt->type == APP_DSPI_EVT_ERROR)
{
}
}

static void app_dspi_display_4w2l_write(uint8_t *data, uint32_t dataLen)
{
// Write_Command(0x2C);
app_dspi_config_mode(DSPI_PROT_MODE_4W1L);
g_cmd.instruction = 0x2C;
g_cmd.instruction_size = DSPI_INSTSIZE_08_BITS;
g_cmd.data_size = DSPI_DATASIZE_16_BITS;
g_cmd.length = 0;

g_master_tdone = 0;
app_dspi_dma_command_async(&g_cmd);
while(g_master_tdone == 0);

display_xfer_remain = dataLen;
display_xfer_point = data;
display_busy = 1;

app_dspi_config_mode(DSPI_PROT_MODE_4W2L);
app_dspi_config_data_size(DSPI_DATASIZE_16_BITS);

if (display_xfer_remain > DSPI_DMA_SIZE)
{
	app_dspi_display_xfer_start(display_xfer_point, DSPI_DMA_SIZE);
	display_xfer_remain -= DSPI_DMA_SIZE;
	display_xfer_point += DSPI_DMA_SIZE;
}
else
{
	app_dspi_display_xfer_start(data, dataLen);
	display_xfer_remain = 0;
	display_xfer_point += dataLen;
}
}



static void app_dspi_display_3w1l_write(uint8_t *data, uint32_t dataLen)
{
// Write_Command(0x2C);
//    g_cmd.instruction = 0x2C;
//    g_cmd.instruction_size = DSPI_INSTSIZE_08_BITS;
//    g_cmd.data_size = DSPI_DATASIZE_16_BITS;
//    g_cmd.length = 0;
//    g_master_tdone = 0;
//    app_dspi_dma_command_async(&g_cmd);

//while(g_master_tdone == 0);
//Solomon_VMD(0x2C);
display_xfer_remain = dataLen;
display_xfer_point = data;
display_busy = 1;

if (display_xfer_remain > DSPI_DMA_SIZE)
{
	app_dspi_display_xfer_start(display_xfer_point, DSPI_DMA_SIZE);
	display_xfer_remain -= DSPI_DMA_SIZE;
	display_xfer_point += DSPI_DMA_SIZE;
}
else
{
	app_dspi_display_xfer_start(data, dataLen);
	display_xfer_remain = 0;
	display_xfer_point += dataLen;
}
}

static uint32_t screen_init(app_dspi_evt_handler_t evt_handler)
{
	uint16_t ret = APP_DRV_ERR_HAL;

	ret = app_dspi_init(&dspi_params, evt_handler);
	if (ret != APP_DRV_SUCCESS)
	{
		printf("Initialize DSPI failed\r\n");
	}
	delay_ms(200);

	ret = app_dspi_dma_init(&dspi_params);
	if (ret != APP_DRV_SUCCESS)
	{
		printf("Initialize DSPI DMA failed\r\n");
	}
	delay_ms(200);

return ret;
}

static void lcd_io_init(app_gpiote_param_t io_para)
{
app_io_init_t io_init;
io_init.pin  = io_para.pin;
io_init.mode = io_para.mode;
io_init.pull = io_para.pull;
#if (APP_DRIVER_CHIP_TYPE != APP_DRIVER_GR5525X)
io_init.mux  = APP_IO_MUX_7;
#else
io_init.mux  = APP_IO_MUX_8;
#endif
app_io_init(io_para.type, &io_init);
}


static void lcd_rst_ctrl(uint8_t level)
{
app_io_pin_state_t pin_state;
app_gpiote_param_t rst_pin = DISP_RST_CONFIG;
app_io_type_t pin_type = rst_pin.type;
uint32_t pin = rst_pin.pin;

pin_state = level ? APP_IO_PIN_SET : APP_IO_PIN_RESET;
app_io_write_pin(pin_type, pin, pin_state);
}
static void qspi_screen_misc_pins_init(void)
{
app_gpiote_param_t io_para1 = DISP_RST_CONFIG;
lcd_io_init(io_para1);
lcd_rst_ctrl(0);
app_gpiote_param_t io_para2 = DISP_TE_CONFIG;
lcd_io_init(io_para2);
app_gpiote_param_t io_para3 = DISP_DC_CONFIG;
lcd_io_init(io_para3);
}

static void lcd_write(uint8_t cmd, uint8_t *data, uint32_t data_len)
{
g_cmd.instruction = cmd;
g_cmd.instruction_size = DSPI_INSTSIZE_08_BITS;
g_cmd.data_size = DSPI_DATASIZE_08_BITS;
g_cmd.length = data_len;

g_master_tdone = 0;
app_dspi_dma_command_transmit_async(&g_cmd, data);
while(g_master_tdone == 0);
}


//static void lcd_cmd_send(uint8_t cmd, uint8_t param)
//{
//    lcd_write(cmd, &param, 1);
//}



static void lcd_ca_ra_set(uint16_t x1, uint16_t x2, uint16_t y1, uint16_t y2)
{
uint8_t data[4];

data[0] = (x1 & 0xff00) >> 8;
data[1] = x1 & 0x00ff;
data[2] = (x2 & 0xff00) >> 8;
data[3] = x2 & 0x00ff;
Solomon_VMD(0x2a);
Solomon_VPA(data[0]);
Solomon_VPA(data[1]);
Solomon_VPA(data[2]);
Solomon_VPA(data[3]);
// lcd_write(0x2a, data, 4);

data[0] = (y1 & 0xff00) >> 8;
data[1] = y1 & 0x00ff;
data[2] = (y2 & 0xff00) >> 8;
data[3] = y2 & 0x00ff;
Solomon_VMD(0x2b);
Solomon_VPA(data[0]);
Solomon_VPA(data[1]);
Solomon_VPA(data[2]);
Solomon_VPA(data[3]);
   // lcd_write(0x2b, data, 4);
}

static void dspi_screen_set_show_area(uint16_t x1, uint16_t x2, uint16_t y1, uint16_t y2)
{
    lcd_ca_ra_set(x1, x2, y1, y2);
}

/*
 * GLOBAL FUNCTION
 ***************************************************************
 *
 */
void display_lcd(uint8_t *data, uint32_t dataLen, uint8_t mode)
{
//    if (mode == D_DISPLAT_4W1L)
//    {
//        app_dspi_display_4w1l_write(data, dataLen);
//    } 
//    else if (mode == D_DISPLAT_4W2L)
//    {
//        app_dspi_display_4w2l_write(data, dataLen);
//    } 
//    else if (mode == D_DISPLAT_3W1L)
    {
        app_dspi_display_3w1l_write(data, dataLen);
    }

    while(display_busy == 1);
}

void dspi_screen_init_basic(uint8_t mode)
{
	screen_init(app_dspi_evt_handler);

	qspi_screen_misc_pins_init();

	lcd_rst_ctrl(0);
	delay_ms(20);
	lcd_rst_ctrl(1);
	delay_ms(10);
	lcd_rst_ctrl(0);
	delay_ms(10);
	lcd_rst_ctrl(1);
	delay_ms(10);

	init_seq();
	
	
	//send_cmd_data(0x2c,240*280*2,frambuffer);
//	lcd_ca_ra_set(0,120,0,50);
	
//	Solomon_VPA(0x8C);
//	Solomon_VMD(0x2E);
	//dspi_screen_set_show_area(0,240,0,240);
    //lcd_cmd_sequence(mode);
}


void  test(uint8_t *data)
{
	Solomon_VMD_2(0x2C00);
	
//app_dspi_display_xfer_start(frambuffer,4094);
//	//app_dspi_dma_transmit_async(frambuffer,240);
//	for(int i=0;i<283;i++)
//	{
//		  g_master_tdone = 0;
//    app_dspi_dma_transmit_async(frambuffer,240*2);
//    while(g_master_tdone == 0);
//		
//		Solomon_VPA_2(0x2C4C);
//		Solomon_VPA(0x4C);
	//app_dspi_display_xfer_start(frambuffer,240);
	app_dspi_display_3w1l_write(data,240*283*2);
	while(display_busy == 1);
}

//void clear_lcd(uint16_t color)
//{
//	for(int i=0;i<240*283;i++)
//		frambuffer[i]
//	screen_fill(0,0,240,283,);
//	

//}	
void screen_fill(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2, uint8_t *color)
{
//	if(app_io_read_pin(APP_IO_TYPE_AON,APP_IO_PIN_0))
	{
		uint8_t data[4];
		data[0] = (x1 & 0xff00) >> 8;
		data[1] = x1 & 0x00ff;
		data[2] = (x2 & 0xff00) >> 8;
		data[3] = x2 & 0x00ff;
		Solomon_VMD(0x2a);
		Solomon_VPA(data[0]);
		Solomon_VPA(data[1]);
		Solomon_VPA(data[2]);
		Solomon_VPA(data[3]);

		data[0] = (y1 & 0xff00) >> 8;
		data[1] = y1 & 0x00ff;
		data[2] = (y2 & 0xff00) >> 8;
		data[3] = y2 & 0x00ff;
		Solomon_VMD(0x2b);
		Solomon_VPA(data[0]);
		Solomon_VPA(data[1]);
		Solomon_VPA(data[2]);
		Solomon_VPA(data[3]);	
		Solomon_VMD(0x2C);
		int len=(x2-x1+1)*(y2-y1+1)*2;
		for(int i=0;i<len/1024;i++)
		{
			g_master_tdone=0;
			app_dspi_dma_transmit_async(color,1024);
			while(!g_master_tdone);
			color=color+1024;
		}
			for(int i=0;i<len%1024;i++)
		{
			app_dspi_dma_transmit_async(color,1);
			color=color+1;
		}
	
//	app_dspi_display_3w1l_write((uint8_t *)color,len);
//	while(display_busy == 1);
	
		Solomon_VMD(0x00);
	}

}

void dspi_screen_deinit(void)
{
    uint16_t ret = APP_DRV_ERR_HAL;
    ret = app_dspi_dma_deinit();
    if (ret != APP_DRV_SUCCESS)
    {
        printf("Deinitialize DSPI DMA failed\r\n");
    }
    
    ret = app_dspi_deinit();
    if (ret != APP_DRV_SUCCESS)
    {
        printf("Deinitialize DSPI failed\r\n");
    }
}
void brightness_level(char level)
{
//	if(level%10==0)
	{
	app_pwm_channel_init_t channel_cfg = {0};
	app_pwm_init(&pwm_params);
	channel_cfg.duty = level;
  channel_cfg.drive_polarity = PWM_DRIVEPOLARITY_POSITIVE;
#if (APP_DRIVER_CHIP_TYPE != APP_DRIVER_GR551X)
  channel_cfg.fstoplvl = PWM_STOP_LVL_HIGH;
#endif
  app_pwm_config_channel(APP_PWM1_MODULE, APP_PWM_ACTIVE_CHANNEL_A, &channel_cfg);

  app_pwm_start(APP_PWM1_MODULE);
}

 // app_pwm_start(APP_PWM1_MODULE);

}
void bg_open()
{
	app_pwm_start(APP_PWM1_MODULE);
}
void adjust_level(char level)
{
	app_pwm_channel_init_t channel_cfg = {0};
	channel_cfg.duty = level;
  channel_cfg.drive_polarity = PWM_DRIVEPOLARITY_POSITIVE;
#if (APP_DRIVER_CHIP_TYPE != APP_DRIVER_GR551X)
  channel_cfg.fstoplvl = PWM_STOP_LVL_HIGH;
#endif
  app_pwm_config_channel(APP_PWM1_MODULE, APP_PWM_ACTIVE_CHANNEL_A, &channel_cfg);

}